package net.martinimix.service.payment.provider.cybersource;

import net.martinimix.domain.order.Order;
import net.martinimix.domain.payment.BillMeLaterAccount;
import net.martinimix.domain.payment.CreditCard;
import net.martinimix.service.payment.provider.cybersource.context.BillMeLaterAuthorizationRequest;
import net.martinimix.service.payment.provider.cybersource.context.CyberSourceServiceRequest;
import net.martinimix.service.payment.provider.cybersource.context.OrderPaymentAuthorizationRequest;

/**
 * Provides a Bill Me Later CyberSource services request adapter.
 *  
 * @author Scott Rossillo
 *
 */
public class BillMeLaterCyberSourceRequestAdapter extends SimpleCyberSourceRequestAdapter {
	
	/**
	 * The default sign up account number for Bill Me Later.
	 */
	public final static String DEFUALT_SIGN_UP_ACCOUNT_NUMBER = "5049900000000000"; 
	
	private String signUpAccountNumber = DEFUALT_SIGN_UP_ACCOUNT_NUMBER;
	
	/**
	 * Creates a new Bill Me Later CyberSource services request adapter.
	 */
	public BillMeLaterCyberSourceRequestAdapter() { }
	
	/**
	 * Returns a new {@link BillMeLaterAuthorizationRequest}.
	 * 
	 * @see net.martinimix.service.payment.provider.cybersource.SimpleCyberSourceRequestAdapter#createRequestCommand()
	 */
	protected OrderPaymentAuthorizationRequest createRequestCommand() {
		return new BillMeLaterAuthorizationRequest();
	}

	/* (non-Javadoc)
	 * @see net.martinimix.service.payment.provider.cybersource.SimpleCyberSourceRequestAdapter#prepareAuthorizationRequest(net.martinimix.domain.payment.CreditCard, net.martinimix.domain.order.Order)
	 */
	public final CyberSourceServiceRequest prepareAuthorizationRequest(CreditCard billMeLaterAccount, Order order) {
		return prepareAuthorizationRequest((BillMeLaterAccount) billMeLaterAccount, order);
	}
		
	/**
	 * Prepares an authorization request for the given Bill Me Later account.
	 * 
	 * @param billMeLaterAccount the <code>BillMeLaterAccount</code> containing a sign-up request
	 * 
	 * @param order the current <code>Order</code>
	 * 
	 * @return a <code>BillMeLaterAuthorizationRequest</code> for the given <code>billMeLaterAccount</code>
	 * and <code>order</code>
	 */
	protected CyberSourceServiceRequest prepareAuthorizationRequest(BillMeLaterAccount billMeLaterAccount, Order order) {
		
		final BillMeLaterAuthorizationRequest request = (BillMeLaterAuthorizationRequest) createRequestCommand();
		
		setupCommonRequestAttributes(request, billMeLaterAccount, order);	
		request.setCustomerRegistrationDate(order.getCustomer().getCreatedDate());
		request.setClientIPAddress(order.getOrderHeader().getIpAddress());
		
		billMeLaterAccount.setCardNumber(signUpAccountNumber);
		request.setBillMeLaterAccount(billMeLaterAccount);
		
		if(super.isSendShippingLineItem()) {
			request.setProductDeliveryType(BillMeLaterAuthorizationRequest.DELIVERY_TYPE_SHIPPING_AND_HANDLING);
		}
		
		return request;
	}
	
	
	/**
	 * Sets the sign-up account number to use when requesting authorizations
	 * for new Bill Me Later customers.
	 * 
	 * @param signUpAccountNumber the sign-up account number for new account
	 * authorizations, typically <code>5049900000000000</code>
	 * or <code>6219930000000000</code>
	 */
	public void setSignUpAccountNumber(String signUpAccountNumber) {
		this.signUpAccountNumber = signUpAccountNumber;
	}

	
}
